package com.milhouz.wormdroid.gl.factory;

import java.util.ArrayList;

import android.util.Log;

import com.milhouz.wormdroid.R;
import com.milhouz.wormdroid.gl.model.BotGLSprite;
import com.milhouz.wormdroid.gl.model.GLSprite;
import com.milhouz.wormdroid.util.StaticValues;

public class TeamFactory {

	private final static String SPRITE_TYPE_BOT = "BOT";
	private final static int DEFAULT_SPRITE_WIDTH = 15;
	private final static int DEFAULT_SPRITE_HEIGHT = 20;
	private final static int DEFAULT_SCREEN_WIDTH_PLACEMENT = 500;
	private final static int DEFAULT_SCREEN_HEIGHT_PLACEMENT = 300;

	public final static ArrayList<String> teamNames = new ArrayList<String>();

	private static TeamFactory instance;

	public static TeamFactory getInstance() {
		if (instance == null) {
			instance = new TeamFactory();
		}
		return instance;
	}

	public GLSprite[] generateTeamsBots(final int nbTeams, final int nbBots) {
		GLSprite[] bots = new BotGLSprite[nbBots * nbTeams];
		final int robotBucketSize = nbBots / 4;
		int xRobot, yRobot;
		int indexBotId = 0;
		int resourceImg;
		for (int teamId = 1; teamId <= nbTeams; teamId++) {
			Log.d("wormdroid", " Team : " + teamId);
			if (teamId == 1)
				resourceImg = R.drawable.smallmario;
			else
				resourceImg = R.drawable.smallmario_2;
			for (int i = 0; i < nbBots; i++) {
				Log.d("wormdroid", " ... Wormdroid : " + indexBotId);
				// Pick a random location for this sprite.
				xRobot = (int) (Math.random() * DEFAULT_SCREEN_WIDTH_PLACEMENT);
				yRobot = (int) (Math.random() * DEFAULT_SCREEN_HEIGHT_PLACEMENT);
				// Our robots come in three flavors. Split them up accordingly.
				if (i < robotBucketSize) {
					bots[indexBotId] = generateBot(indexBotId, teamId, xRobot, yRobot, DEFAULT_SPRITE_WIDTH, DEFAULT_SPRITE_HEIGHT, resourceImg);
				} else if (i < robotBucketSize * 2) {
					bots[indexBotId] = generateBot(indexBotId, teamId, xRobot, -yRobot, DEFAULT_SPRITE_WIDTH, DEFAULT_SPRITE_HEIGHT, resourceImg);
				} else if (i < robotBucketSize * 3) {
					bots[indexBotId] = generateBot(indexBotId, teamId, -xRobot, yRobot, DEFAULT_SPRITE_WIDTH, DEFAULT_SPRITE_HEIGHT, resourceImg);
				} else {
					bots[indexBotId] = generateBot(indexBotId, teamId, -xRobot, -yRobot, DEFAULT_SPRITE_WIDTH, DEFAULT_SPRITE_HEIGHT, resourceImg);
				}
				indexBotId++;
			}
		}
		return bots;
	}

	public BotGLSprite generateBot(int botId, int teamId, int x, int y, int witdh, int height, int resourceId) {
		BotGLSprite bot = new BotGLSprite(botId, teamId, x, y, witdh, height, resourceId);
		bot.mSpriteType = SPRITE_TYPE_BOT;
		StaticValues.spriteId = botId + 1;
		return bot;
	}

}
